using Furion.DatabaseAccessor;

using Microsoft.EntityFrameworkCore;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

using Tool;

namespace Abc.Core.Entities
{
    /// <summary>
    /// 菜单表
    /// </summary>
    [Comment("菜单表")]
    [TablePrefix("Web.")]
    public class Menu : FullEntity
, IEntityTypeBuilder<Menu>
, IEntitySeedData<Menu>
    {
        /// <summary>
        /// 菜单ID，全局唯一标识
        /// </summary>
        [Comment("菜单ID")]
        public string MId { get; set; }

        /// <summary>
        /// 父Id
        /// </summary>
        [Comment("父Id")]
        public string? PMId { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        [Comment("菜单名称")]
        public string Name { get; set; }

        /// <summary>
        /// 是否可用
        /// </summary>
        [Comment("是否可用")]
        public bool IsEnable { get; set; }

        /// <summary>
        /// 图标
        /// </summary>
        [Comment("图标")]
        public string Icon { get; set; }

        /// <summary>
        /// 顺序(按从小到大排序)
        /// </summary>
        [Comment("顺序")]
        public int Sort { get; set; }

        /// <summary>
        /// Url
        /// </summary>
        [Comment("Url")]
        public string Url { get; set; }

        /// <summary>
        /// 打开方式（字典 0无 1组件 2内链 3外链）
        /// </summary>
        public MenuOpenType OpenType { get; set; }

        /// <summary>
        ///备注
        /// </summary>
        [Comment("备注")]
        public string Remark { get; set; }

        /// <summary>
        /// 是否展开菜单
        /// </summary>
        [Comment("是否展开菜单")]
        public bool IsExpand { get; set; }

        /// <summary>
        /// 节点类型(菜单、工具栏)
        /// </summary>
        [Comment("节点类型(菜单、工具栏)")]
        public MenuType MenuType { get; set; }

        /// <summary>
        /// 是否废止
        /// </summary>
        public bool IsDisuse { get; set; }

        /// <summary>
        /// 是否系统菜单,系统菜单禁止删除
        /// </summary>
        [Comment("是否系统菜单")]
        public bool IsSystem { get; set; }

        /// <summary>
        /// 是否显示菜单
        /// </summary>
        [Comment("是否显示")]
        public bool IsShow { get; set; }

        /// <summary>
        /// 权限
        /// </summary>
        public ICollection<Permission> Permissions { get; set; }

        /// <summary>
        /// 菜单权限中间表
        /// </summary>
        public ICollection<MenuPermission> MenuPermissions { get; set; }

        //  public ICollection<Role> Roles { get; set; }

        public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<Menu> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            //  entityBuilder.HasMany(o => o.Roles).WithMany(o => o.Menus);
            entityBuilder.HasMany(o => o.Permissions).WithMany(o => o.Menus).UsingEntity<MenuPermission>(
                           u => u.HasOne(c => c.Permission).WithMany(c => c.MenuPermissions).HasForeignKey(c => c.PermissionId).OnDelete(DeleteBehavior.Restrict)
                           , u => u.HasOne(c => c.Menu).WithMany(c => c.MenuPermissions).HasForeignKey(c => c.MenuId).OnDelete(DeleteBehavior.Restrict)
                           , u =>
                           {
                               u.HasKey(c => new { c.PermissionId, c.MenuId });
                               //u.HasData(new {AdminId=1,RoleId=1 })//数据种子
                               // u.Property(pt => pt.CreationTime).HasDefaultValueSql("CURRENT_TIMESTAMP"); //每种数据库支持不一样
                           }
                       );
        }

        public IEnumerable<Menu> HasData(DbContext dbContext, Type dbContextLocator)
        {
            //,ParentId=1,Url=""
            var menus = new List<Menu>()
            {
                new Menu(){Id=1,MId="SystemManager",MenuType=MenuType.目录,Name="系统管理",Icon="layui-icon-set-fill",IsEnable=true,IsShow=true,Sort=1,PMId=""},
                new Menu(){Id=2,MId="UserManager",MenuType=MenuType.目录,Name="用户管理",Icon="layui-icon-user",IsEnable=true,IsShow=true,Sort=2,PMId=""},
                new Menu(){Id=3,MId="LogRecord",MenuType=MenuType.目录,Name="日志记录",Icon="layui-icon-log",IsEnable=true,IsShow=true,Sort=5,PMId=""},
                new Menu(){Id=4,MId="StatisticsManager",MenuType=MenuType.目录,Name="数据统计",Icon="layui-icon-chart",IsEnable=true,IsShow=true,Sort=6,PMId=""},
                new Menu(){Id=5,MId="DeveloperManager",MenuType=MenuType.目录,Name="开发工具",Icon="layui-icon-util",IsEnable=true,IsShow=true,Sort=7,PMId=""},

                new Menu(){Id=11000,MId="API",MenuType=MenuType.API,Name="API接口",Icon="layui-icon-util",IsEnable=true,IsShow=true,Sort=1,PMId=""},


            };
            foreach (var menu in menus)
            {
                menu.Icon = $"layui-icon { menu.Icon}";
            }

            return menus;
        }
    }

    /// <summary>
    /// 菜单打开类型
    /// </summary>
    public enum MenuOpenType
    {
        内链 = 0,
        外链 = 1,
    }
}